<template>
  <div>
    <el-dialog title="添加" :visible.sync="dialogVisible" width="500px">
      <el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-width="100px" class="demo-ruleForm">
        <el-form-item label="图片" prop="imageUrl">
          <el-upload class="avatar-uploader" :action="actionUrl" :show-file-list="false"
            :headers="{ 'Authorization': Authorization }" :on-success="handleAvatarSuccess">
            <img v-if="ruleForm.imageUrl" :src="ruleForm.imageUrl" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon" />
          </el-upload>
        </el-form-item>
        <el-form-item label="链接地址" prop="targetUrl">
          <el-input v-model="ruleForm.targetUrl" placeholder="输入内容" />
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input v-model="ruleForm.sort" placeholder="请输入排序" />

        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')">立即创建</el-button>
          <!-- <el-button @click="resetForm('ruleForm')">重置</el-button> -->
        </el-form-item>
      </el-form>
    </el-dialog>

  </div>
</template>
<script>
import { bannerSave, action } from '@/api/table'
import { getToken } from '@/utils/auth'

export default {

  data() {
    return {
      dialogVisible: false,
      listLoading: true,
      total: 0,
      actionUrl: '',
      fileList: [],
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: ''
      },
      ruleForm: {
        imageUrl: '',
        targetUrl: '',
        sort: ''
      },
      Authorization: '',
      rules: {
        imageUrl: [
          { required: true, message: '请上传banner', trigger: 'change' }
        ],
        targetUrl: [
          { required: true, message: '请输入链接地址', trigger: 'blur' }
        ],
        sort: [
          { required: true, message: '请输入排序', trigger: 'blur' }
        ]

      }

    }
  },
  created() {

  },
  methods: {

    async show(record) {
      this.dialogVisible = true
      this.actionUrl = action()
      this.Authorization = 'Bearer ' + getToken()
      this.$refs['ruleForm'] && this.$refs['ruleForm'].resetFields()
      this.ruleForm = {
        imageUrl: '',
        targetUrl: '',
        sort: '',
        id: ''
      }
    },
    async edit(record) {
      this.dialogVisible = true
      this.actionUrl = action()
      this.Authorization = 'Bearer ' + getToken()
      this.$refs['ruleForm'] && this.$refs['ruleForm'].resetFields()

      this.ruleForm = record
    },
    handleAvatarSuccess(e) {
      this.ruleForm.imageUrl = e.data

    },
    submitForm(formName) {

      this.$refs[formName].validate((valid) => {
        if (valid) {
          const loading = this.$loading({
            lock: true,
            text: 'Loading',
            spinner: 'el-icon-loading',
            background: 'rgba(0, 0, 0, 0.7)'
          });

          bannerSave(this.ruleForm).then(res => {
            this.dialogVisible = false
            this.$parent.fetchData()
            setTimeout(() => {
              loading.close();
            }, 2000);
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    }

  }
}
</script>
<style scoped lang="scss">
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}

.el-tag+.el-tag {
  margin-left: 10px;
}

.button-new-tag {
  margin-left: 10px;
  height: 32px;
  line-height: 30px;
  padding-top: 0;
  padding-bottom: 0;
}

.input-new-tag {
  width: 90px;
  margin-left: 10px;
  vertical-align: bottom;
}

.list {
  .item {
    border: 1px solid #c0ccda;
    border-radius: 5px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 10px;
    margin-top: 10px;

    >div {
      display: flex;

      img {
        margin-right: 15px;
      }
    }

    >i {
      font-size: 20px;
      cursor: pointer;
    }
  }
}
</style>
